Stat 290: Git Merge Conflicts

Exercise 03

Goal: Merge the changes we made in branch1 into master branch. We will use the ex02 repo we created previously.

NOTE: This is done on the website!

  1. On GitLab repo, choose the Merge Requests option on left and click on New merge request.

You will get the following dialog.

  1. Since we want to merge branch1 into master, ensure that your page looks as below showing the source and target branches.

  1. Click on Compare branches and continue to get the dialog below. Note how I have added some text describing the changes. Go ahead and Submit merge request

  1. This is the ensuing dialog. By default, it gives you the option to delete the source branch, but I have unchecked it here, to retain it for use later, as we shall see.

  1. When you click on the green Merge button, you will get the following dialog with the checkbox indicating that the merge has been made.

  1. (Optional) If you now go back to your project website ex02 on top left, you will see that the master branch has all the changes that belonged to branch1, i.e. both are identical.

Exercise 04

Let us go back to ex02 and work further on branch1.

  1. In Rstudio, switch to master branch. Notice that you don’t see the merged changes. Use Pull in the Git tab to sync up: you will see the files change.

  1. Switch to branch1 in Rstudio and make further edits to README.md by adding Edited a bit more after merge request.

  2. Commit with message Commit after Merge Request and push changes.

  3. Check that GitLab reflects the changes to branch1.

  4. Switch back to master branch on Rstudio and create another branch branch2 off the master branch.

  5. Now edit the file README.md again and add a line Update from branch2.

  6. Commit with message Commit from branch2 and push branch2.

  1. On GitLab you should now see three branches: master, branch1, branch2.

  1. Make a Merge Request from branch2 into master on the website and complete the merge with message Merge Request from Branch2.

Submit the merge request.

Go ahead and merge it but once again, do not delete source branch.

  1. In Rstudio, use Pull to pull in the changes. Notice how branch2 changes are now in master.

  2. Go back to GitLab and make a Merge Request from branch1 into master on the website and try to complete the merge with message Merge from Branch1 after Branch2.

Go ahead and merge it but do not delete source branch.

  1. The merge will fail! Notice that the Merge button is not green. You have to resolve the conflicts!

What happened? Here is the picture that helps.

  1. You can click on Resolve conflicts to resolve the conflict.

  1. If you choose Edit inline you will see the are always highlighted using the
<<<<<<<<<<<

and

>>>>>>>>>>>

delimeters. We reconcile it and note it and mark as resolved.

  1. After resolving the conflict, go ahead and merge.

  1. You can now fetch and pull from the origin in Rstudio to have sanity back.

  2. You can also delete the branches, now that they have served their purpose.

Notes on conflicts

The conflict resolution issue is what I get asked most about especially by team members on a project.

Workflow Recommendation

I create a branch whenever I want to make some feature changes. Making changes in my branch is a good way to create my edits away from the master branch.

If you don’t use branches, you are working directly with the master branch.

To create a new branch, I execute

git branch mybranch

which creates a local branch named mybranch.

Now I have to inform the remote repo that I have a new branch as shown below.

git push --set-upstream origin mybranch

Now I can do all my work with my branch as follows, first by checking it out.

git checkout mybranch

I then make my changes as usual and do commits and push changes upstream as outlined earlier. When I push my branch to the remote repo, I can choose to merge my branch into the master. I have to approve it and in the process resolve any conflicts that may arise. Once it is approved, stuff from my branch, gets merged into the master branch.

Once done with a branch, I can delete it.

git branch -d mybranch

This does not affect the changes that have already been merged into the master via my merge request.

Important Rstudio note

When you delete branches on remote repository, Rstudio, not being a full Git IDE, can sometimes get confused.

In such cases, I quite Rstudio, and if I am sure that the remote repository is fully updated and current, I would either rename my project directory and clone the repo again to synchronize.

Yes, unfortunately, Rstudio only goes so far.

References